package com.yizhuoyan.his.serviceimpl.doctor.outpatient;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yizhuoyan.his.ao.CaseDiagnoseResultAo;
import com.yizhuoyan.his.ao.CaseMedicalRecordAo;
import com.yizhuoyan.his.dao.CaseDiagnoseResultDao;
import com.yizhuoyan.his.dao.CaseMedicalRecordDao;
import com.yizhuoyan.his.dto.QueryResult;
import com.yizhuoyan.his.entity.CaseDiagnoseResultEntity;
import com.yizhuoyan.his.entity.CaseMedicalRecordEntity;
import com.yizhuoyan.his.exception.HISException;
import com.yizhuoyan.his.service.doctor.outpatient.CaseOutpatientMedicalRecordService;
import com.yizhuoyan.his.servicesupport.DatabaseIdGenerateSupportService;
import com.yizhuoyan.his.util.BeanMap;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service
public class CaseOutpatientMedicalRecordServiceImpl implements CaseOutpatientMedicalRecordService {

    @Autowired
    CaseMedicalRecordDao cmrDao;
    @Autowired
    CaseDiagnoseResultDao cdrDao;
    @Autowired
    DatabaseIdGenerateSupportService databaseIdGenerateSupportService;
    @Override
    public void addCaseMedicalRecord(@NonNull CaseMedicalRecordAo ao) throws Exception {
        //1参数验证
        String caseNo=ao.getCaseNo();
        if(caseNo==null && caseNo.trim().length()==0){
            throw new HISException("error","病历号输入有误");
        }

        CaseMedicalRecordEntity e=new CaseMedicalRecordEntity();
        e.setId(databaseIdGenerateSupportService.nextId());
        e.setAllergyHistoryDetail(ao.getAllergyHistoryDetail());
        e.setCarefulDetail("123");
        e.setCaseNo(caseNo);
        e.setCaseState(1);
        e.setCheckResultDetail("123");
        e.setDiagnosisResultDetail("123");
        e.setHandlingDetail("123");
        e.setPhysiqueDetail(ao.getPhysiqueDetail());
        e.setPresentDetail(ao.getPresentDetail());
        e.setPresentTreatmentDetail(ao.getPresentTreatmentDetail());
        e.setProposalDetail(ao.getProposalDetail());
        e.setReadme(ao.getReadme());
        e.setRegistrationId(ao.getRegistrationId());
        e.setSickHistoryDetail(ao.getSickHistoryDetail());
        cmrDao.insert(e);
    }

    @Override
    public void addCaseDiagnoseResult(@NonNull CaseDiagnoseResultAo ao) throws Exception {
        //参数验证
        //业务逻辑
        CaseDiagnoseResultEntity e=new CaseDiagnoseResultEntity();
        e.setCaseMedicalId(ao.getCaseMedicalId());
        e.setCaseRegistrationId(ao.getCaseRegistrationId());
        e.setDiagnoseStatus(ao.getDiagnoseStatus());
        e.setDiagnoseType(ao.getDiagnoseType());
        e.setDiseaseId(ao.getDiseaseId());
        e.setGotSickDate(ao.getGotSickDate());
        e.setId(databaseIdGenerateSupportService.nextId());
        cdrDao.insert(e);
    }

    @Override
    public QueryResult<CaseMedicalRecordEntity> queryHistoryCaseByCaseID(String caseID, int pageNo, int pageSize) throws Exception {
        //验证参数
        if(caseID==null && caseID.trim().length()==0){
            throw new HISException("error","病历号输入有误");
        }
        if (pageNo <= 0) {
            pageNo = 1;
        }
        if (pageSize <= 0) {
            pageSize = 5;
        }
        CaseMedicalRecordEntity e=cmrDao.selectOne("case_no",caseID);
        PageHelper.startPage(pageNo, pageSize);
        List<CaseMedicalRecordEntity> list = cmrDao.selectByCaseId(e.getId());
        PageInfo<CaseMedicalRecordEntity> page = new PageInfo<>(list);
        QueryResult<CaseMedicalRecordEntity> result = new QueryResult<CaseMedicalRecordEntity>();
        result.setPageNo(pageNo);
        result.setRows(page.getList());
        result.setTotalRows(page.getTotal());
        result.setPageSize(pageSize);
        return result;
    }

    @Override
    public void modifyDiagnosis(Long id, Integer diagnoseStatus) throws Exception {

        if(diagnoseStatus!=1 && diagnoseStatus!=2){
            throw new HISException("error","诊断状态有误");
        }
        cdrDao.update(id, BeanMap.ofOnly("diagnose_status",diagnoseStatus));
    }
}
